<?php
require_once('./lib/nusoap.php');
/*
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';
$client = new nusoap_client('http://card.gate.vn/api/Fo_ws/srvCard.asmx?wsdl', 'wsdl',
						$proxyhost, $proxyport, $proxyusername, $proxypassword);
*/		
$client = new nusoap_client('http://card.gate.vn/api/Fo_ws/srvCard.asmx?wsdl', 'wsdl');			
$err = $client->getError();
if ($err) {
	echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
// Doc/lit parameters get wrapped
$UserName = "onhamotminh3";
$Serial   = "FA00000036";
$PIN      = "1234567890";
$PartnerID= "35";
$Signature ="";

//Begin digital sign
$OriginalData  = $UserName.$Serial.$PartnerID.$PIN;
echo $OriginalData;
$tmp_path = tempnam("tmp/", 'pem');
file_put_contents($tmp_path . '_data.txt', $OriginalData);
$clearFile =  $tmp_path . '_data.txt';
$signedFile = $tmp_path . '_signed.txt';
$public_key = "server.crt";
$private_key = "server.key";

if (openssl_pkcs7_sign($clearFile,$signedFile,file_get_contents($public_key), array(file_get_contents($private_key),"123456"), array() )) 
{   
	  $signedData = explode("\n\n",file_get_contents($signedFile));	  
	  @unlink($tmp_path);
	  @unlink($clearFile);
	  @unlink($signedFile);		  
	  $Signature =  $signedData[3];	
}
//End digital sign

$param = array(
			'UserName' => $UserName,
			'Serial'   => $Serial,
			'PIN'      => $PIN,
			'PartnerID'=> $PartnerID,
			'ClientIP' => 'localhost', 
			'Signature'=> $Signature 
		);
$result = $client->call('CardInput', $param, '', '', false, true);
// Check for a fault
if ($client->fault) {
	echo '<h2>Fault</h2><pre>';
	print_r($result);
	echo '</pre>';
} else {
	// Check for errors
	$err = $client->getError();
	if ($err) {
		// Display the error
		echo '<h2>Error</h2><pre>' . $err . '</pre>';
	} else {
		// Display the result
		echo '<h2>Result</h2><pre>';
		//print_r($result);		
		echo $result["CardInputResult"]["anyType"][0]."<br/>";
		echo $result["CardInputResult"]["anyType"][1]."<br/>";
		echo $result["CardInputResult"]["anyType"][2]."<br/>";
		echo $result["CardInputResult"]["anyType"][3]."<br/>";
		
		echo '</pre>';
	}
}
echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
//echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
